package com.cskaoyan.service;

import com.cskaoyan.bean.pojo.Brand;
import com.cskaoyan.bean.pojo.BrandExample;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.BrandListVO;
import com.cskaoyan.bean.vo.wx.BrandBean;
import com.cskaoyan.bean.vo.wx.BrandDetailVO;
import com.cskaoyan.bean.vo.wx.BrandListBean;
import com.cskaoyan.dao.BrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ckd
 * @description 微信品牌模块
 */
@SuppressWarnings("all")
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    BrandMapper brandMapper;

    @Override
    public BrandListVO brandList(int page, int size) {
        PageHelper.startPage(page, size);
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        List<BrandListBean> brandList = new ArrayList<>();
        for (Brand brand : brands) {
            brandList.add(new BrandListBean(brand.getId(), brand.getName(), brand.getDesc(), brand.getPicUrl(), brand.getFloorPrice().doubleValue()));
        }
        int count = (int) brandMapper.countByExample(brandExample);
        return new BrandListVO(count, brandList);
    }

    @Override
    public BrandDetailVO brandDetail(int id) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        BrandExample brandExample = new BrandExample();
        Brand brand = brandMapper.selectByPrimaryKey(id);
        BrandBean brandBean = new BrandBean(brand.getId(), brand.getName(), brand.getDesc(),
                brand.getPicUrl(), brand.getSortOrder(), brand.getFloorPrice().doubleValue(), sdf.format(brand.getAddTime()), sdf.format(brand.getUpdateTime()), brand.getDeleted());
        return new BrandDetailVO(brandBean);
    }
}
